package com.yanqu.road.dao.impl.player;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;

import com.yanqu.road.entity.winery.UserWineryData;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class UserWineryDaoImpl extends TempDataObjectDao<UserWineryData> {

    public static String getUserWineryDataSql = "select * from `t_u_winery` where `userId`=? and `wineryId`=?";
    public UserWineryData getUserWineryData(long userId,int wineryId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserWineryDataSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, wineryId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserWineryDataSql = "update `t_u_winery` set level=?,createAt=?,updateAt=? where userId=? and wineryId=?";
    @Override
    protected boolean updateData(UserWineryData userWineryData) {
        userWineryData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userWineryData.getLevel()));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userWineryData.getCreateAt().getTime())));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userWineryData.getUpdateAt().getTime())));
        para.put(i++, new DbParameter(Types.BIGINT, userWineryData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryData.getWineryId()));
        if (getDb().executeNoneQuery(updateUserWineryDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserWineryDataSql = "insert into `t_u_winery`(userId,wineryId,level,createAt,updateAt) values (?,?,?,?,?)";
    @Override
    protected boolean addData(UserWineryData userWineryData) {
        userWineryData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userWineryData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryData.getWineryId()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryData.getLevel()));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userWineryData.getCreateAt().getTime())));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userWineryData.getUpdateAt().getTime())));
        if (getDb().executeNoneQuery(insertUserWineryDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserWineryData parseData(ResultSet rs) throws SQLException {
        UserWineryData userWineryData = new UserWineryData();
        userWineryData.setUserId(rs.getLong("userId"));
        userWineryData.setWineryId(rs.getInt("wineryId"));
        userWineryData.setLevel(rs.getInt("level"));
        userWineryData.setCreateAt(rs.getTimestamp("createAt"));
        userWineryData.setUpdateAt(rs.getTimestamp("updateAt"));

        return userWineryData;
    }
    public Map<Integer, UserWineryData> getUserWineryDataMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserWineryData> userWineryDataMap = new ConcurrentHashMap<>();
        String selectSql = "select * from t_u_winery where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserWineryData userWineryRegionData = parse(rs);
                userWineryDataMap.put(userWineryRegionData.getWineryId(), userWineryRegionData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return userWineryDataMap;
    }
}
